// pages/link/link.js
const config = require('../../config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fullLoading: true,
    linkLoding: true,
    linkData: [],
    total: 0,
    pageIndex: 1,
    link: '',
    showCopyLink: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '友情链接'
    });
    this.getLinkList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      fullLoading: false
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.total <= that.data.linkData.length) {
      return;
    }
    that.setData({
      pageIndex: that.data.pageIndex + 1
    });
    that.getLinkList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onShareTimeline(res) {
    return {
      title: config.miniName + '-友情链接',
      path: '/pages/link/link'
    }
  },

  getLinkList: function () {
    let that = this;
    that.setData({
      linkLoding: true
    });
    wx.request({
      url: config.basePath + '/mini/link/list',
      method: 'GET',
      data: {
        pageIndex: that.data.pageIndex,
        pageSize: 6
      },
      success(res) {
        if (res.data.code === 200) {
          that.setData({
            linkData: that.data.linkData.concat(res.data.data),
            total: res.data.total,
            linkLoding: true
          });

          if (that.data.total <= that.data.linkData.length) {
            that.setData({
              linkLoding: false
            });
          }
        } else {
          wx.lin.showMessage({
            type: 'error',
            content: '服务出错'
          })
        }
      },
      fail(err) {
        that.setData({
          linkLoding: false
        });
        wx.lin.showMessage({
          type: 'error',
          content: '网络出错'
        })
      }
    });
  },
  goLink: function (event) {
    let that = this;
    that.setData({
      link: event.currentTarget.dataset.link,
      showCopyLink: true
    });
  },
  copyLink: function () {
    let that = this;
    wx.setClipboardData({
      data: that.data.link,
      success: function (res) {}
    })
  },
})